package kalambury;

import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.PBEKeySpec;
import java.io.UnsupportedEncodingException;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.KeySpec;
import java.util.Random;

/**
 * Created with IntelliJ IDEA.
 * User: damienix
 * Date: 20.11.12
 * Time: 00:21
 * To change this template use File | Settings | File Templates.
 */

/**
 * klasa zawierająca pomocnicze funkcje
 */
public abstract class KalamburyUtils {

    /**
     * pobiera skrót SHA1 do celów przechowywania
     * @param string orginalny tekst
     * @return wygenerowany skrót
     */
    public static String getHash(String string) {
        try {
            byte[] salt = new byte[]{26,86,-126,48,
                                     13,-48,31,46,
                                     48,-31,78,-123,
                                     13,57,-31,89};
            KeySpec spec = new PBEKeySpec(string.toCharArray(), salt, 2048, 160);
            SecretKeyFactory f = null;
            f = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
            byte[] hash = f.generateSecret(spec).getEncoded();
            System.out.println("salt: " + new BigInteger(1, salt).toString(16));
            System.out.println("hash: " + new BigInteger(1, hash).toString(16));
            return new BigInteger(1, hash).toString(16).toString();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (InvalidKeySpecException e) {
            e.printStackTrace();
        }
        return null;
    }
}
